const express=require("express")
// const jwt=require("jsonwebtoken")
const timeStamp=require("time-stamp")
let router=express.Router()



// console.log(timeStamp("YYYYMMDDHHmmssms"));

// console.log(  (Math.random()+"").substring(2,8)  );




const query=require("../tools/db")
const {api_url,verifytoken}=require("../config/config")




// 课程详情
router.get("/courseinfo",async (req,res,next)=>{

    // 我们需要知道用户请求的是哪个课程
    // 所以需要传递一个参数，这个参数可以唯一识别某课程 （例如：cid）
    let {cid}=req.query
    if(!cid){
        next("请选择你要查询的课程")
        return
    }

    // 根据cid去数据库里面查询详情信息

    let sql=`select 
                    id ,
                    cid,
                    title,
                    price,
                    intro,
                    concat("${api_url}","/uploads/","videoDetail/",detail_imgs) as detail_imgs,
                    area_name,
                    grade_name,
                    subject_name
            from e_course 
            where cid="${cid}"`

    let [err,results]=await query(sql)

    if(err){
        next("课程详情请求失败")
        return
    }


    if(results.length==0){
        next("没有此课程")
        return
    }


    res.send({
        status:200,
        msg:"课程详情请求成功",
        results
    })

})

// 老师详情
router.get("/teacherinfo",async (req,res,next)=>{
    // 参数，课程id 某个课程的老师
    let {cid}=req.query
    if(!cid){
        next("请求选择课程")
        return
    }

    // 连表查询，两个表的关联就是tid
    // 
    let sql=`select 
                    realname,
                    e_teacher.intro,
                    concat("${api_url}","/uploads/","login/",head_photo_url) as head_photo_url
            from e_course 
            join e_teacher 
            on e_course.tid=e_teacher.tid
            where cid="${cid}"
            `

    let [err,results]=await query(sql)
    
    if(err){
        next("老师信息请求出错")
        return
    }
    
    if(results.length==0){
        next("课程老师信息请求失败，请检查是否是正确的课程")
        return
    }

    res.send({
        status:200,
        msg:"老师信息请求成功",
        results
    })
})


//课程视频列表
router.get("/videolist",async (req,res,next)=>{

    // 根据课程id  去请求此课程都有哪些视频
    let {cid}=req.query
    if(!cid){
        next("请求选择课程")
        return
    }


    let sql=`select  
                    id,
                    cid,
                    video_title,
                    vnum
             from e_video 
             where cid="${cid}"`
    let [err,results]=await query(sql)

    if(err){
        next("课程视频列表请求失败")
        return
    }
    if(results.length==0){
        next("暂无视频")
        return
    }

    res.send({
        status:200,
        msg:"课程视频列表请求成功",
        results
    })
})






module.exports=router